$(function(){
          function Dataoperator(){}
           var dataOperator = new Dataoperator();
          dataOperator.showTable = function(data){
              var table = $("#datashow");
                var trf = table.children()[0];
                 table.empty();
                 table.append(trf);
                 data.forEach(item=>{
                   var tr = $("<tr></tr>");
				   var idtd = $("<td>"+item.id+"</td>");
                   var nametd = $("<td>"+item.name+"</td>");
                   var agetd = $("<td>"+item.age+"</td>");
                   var delbt = $("<button>删除</button>");
                   var deletetd = $("<td></td>");
                    delbt.attr("class","del").attr("id",item.id);
                    deletetd.append(delbt);
				    tr.append(idtd);
                    tr.append(nametd);
                    tr.append(agetd);
                    tr.append(deletetd);
                    table.append(tr);
                 }); 
          }
          
          dataOperator.addDelEvent = function(){
               $(".del").on("click",function(){
                    var $this = $(this);
                      var id = $(this).attr("id");
                      $.ajax({
                          type:'DELETE',
                           url:'http://localhost:3000/users/'+id
                      }).done(function(msg,textstatus){
                          //alert(textstatus);
                         $this.parent().parent().remove();
                      }).fail(function(msg,textstatus){
                          alert("不存在该条数据");
                      });
                    });
          }
           
          $("#loadbt").on("click",function(){
              search(dataOperator);
          });
           
           function search(dataOperator){
               $.ajax({
                type:'GET',
                 url:'http://localhost:3000/users'
                 
             }).done(function(msg){
                 dataOperator.showTable(msg);
                 dataOperator.addDelEvent();
            });
           }
                     
              
}); 







